home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / fweb / fweb-1.40 / manual / f90_cpp.web < prev    next >
Text File  |  1993-10-29  |  3KB  |  119 lines

  1. @z --- f90_cpp.web ---
  2.  
  3. This file is included in fwebman.tex.
  4.  
  5. Author: J. A. Krommes
  6. Version: 1.23
  7. Date:   April 1, 1992
  8.  
  9. @x-----------------------------------------------------------------------------
  10.  
  11. @r9[-r/ -W[]
  12. @* INTRODUCTION.  This example demonstrates operator overloading in both
  13. Ratfor--90 and~C$++$.  It also shows how brackets may be used instead of
  14. parentheses for \Fortran\ array indices.
  15. @l "\\let\\WARRAY\\WSUB" // Subscript \Fortran\ indices.
  16. @a
  17. @<Ratfor--90@>@;
  18. @c++
  19. @<C$++$@>@;
  20.  
  21. @ The following example is excerpted from the ANSI Draft~S8, Version~112
  22. for the Fortran--90 language.
  23.  
  24. @v .IN. "\\in" < /* Make \.{.IN.} display as~`$\in$' and be treated as a
  25.             relational operator. */
  26. @v <= "\\subset" <= /* Make \.{.LE.} display as~`$\subset$' and be treated
  27.             as a relational operator. */
  28.  
  29. @<R...@>=
  30.  
  31. module integer_sets
  32. {
  33. integer, parameter :: max_set_card = 200;
  34.  
  35. type set
  36.     {
  37.   private:
  38.     integer card;
  39.     integer element[max_set_card];
  40.     };
  41.  
  42. interface operator(.IN.)
  43.     {
  44.     module procedure element;
  45.     };
  46.  
  47. interface operator(<=)
  48.     {
  49.     module procedure subset;
  50.     };
  51.  
  52. @<Union function@>@;
  53. @<Subset function@>@;
  54. }
  55.  
  56. @ This function uses structure elements.  We overload the element
  57. operator~`\.{\%}' to make the resulting code look more like~\C.
  58.  
  59. @v % "." . // Make \.\% print as~`\..' and also be treated as~`\..'.
  60.  
  61. @<Union function@>=
  62.  
  63. function union(A,B)
  64.     type(set) A,B;
  65. {
  66. type(set) UNION;
  67. integer j;
  68.  
  69. UNION = A;
  70. do j=1,B%card;
  71.     if(!(B%element[j] .IN. A))
  72.         if(UNION%card < max_set_card)
  73.             {
  74.             UNION%card += 1;
  75.             UNION%element[UNION%card] =
  76.                 B%element[j];
  77.             }
  78.         else ; // Maximum set size exceeded...
  79. }
  80.  
  81. @ We claim that this is much more visually appealing than a raw listing.
  82. @<Subset function@>=
  83.  
  84. logical function subset(A,B)
  85.     type(set) A,B;
  86. {
  87. integer i;
  88.  
  89. subset = A%card <= B%card; /* In the source, this is ``\.{subset\ =\ 
  90.                 A\%card\ <=\ B\%card;}'' */
  91. if(!subset) return;
  92.  
  93. do i=1,A%card;
  94.     subset = subset && (A%element[i] .IN. B);
  95. }
  96.  
  97. @ Here is a short example of operator overloading in~\Cpp.  Note that since the
  98. global language is \Ratfor--90, we must explicitly insert a language
  99. command in the following definition section in order that the star be
  100. overloaded in the proper language.  
  101. @c++
  102. @v * "\\times" *
  103. @<C...@>=
  104.  
  105. class complex
  106.     {
  107.     double re,im;
  108.  
  109.   public:
  110.     complex(double r,double i) {re=r; @+ im=i;}
  111.     friend complex operator+(complex, complex);
  112.     friend complex operator*(complex, complex);
  113.     };
  114.  
  115. z = x*y; /* An example of a statement using the overloading multiplication
  116.         operator. */
  117.  
  118. @* INDEX.
  119.